package dicky.fortunetelling;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import dicky.fortunetelling.logic.CommonUtil;
import dicky.fortunetelling.logic.DataArray;
import dicky.fortunetelling.logic.DatabaseUtil;


import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class nametofortune extends Activity {
	private Button btn1;
	private EditText lastname,name;
	private TextView text,title1,title2,title3,c,five,text1;
	private ListView listView;
	private int c1,c2,c3,c4,c5,l1,l2,n1,n2;
	private String f_name;
	private List list=null;
	private ViewAdapter dataAdapter;
	private static SQLiteDatabase db;
	
	
	private String tag=nametofortune.class.getSimpleName();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nametofortune);

        text=(TextView)findViewById(R.id.text);
        
        lastname=(EditText)findViewById(R.id.lastname);
        name=(EditText)findViewById(R.id.name);
        
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				nametoStrokes();
			}
		});

		listView = (ListView) findViewById(R.id.listview);
        
    }

	private void nametoStrokes(){
		try {
			c1=0;c2=0;c3=0;c4=0;c5=0;l1=0;l2=0;n1=0;n2=0;
			String l=lastname.getText().toString();
			String n=name.getText().toString();
			if (l.length()==0 || n.length()==0){
				Toast.makeText(nametofortune.this, "请输入姓名", Toast.LENGTH_LONG).show();
				return;
			}
			int type = 0;
			String str="";
			String kxstr="";
			JSONObject obj=null;
			//单姓单名
			if (l.length()==1 && n.length()==1){
				type=0;
				obj=DataArray.GetWordJson(nametofortune.this,l);
				f_name=obj.getString("five");
				l1=obj.getInt("kxnum");
				if (l1==0){
					str+="".equals(str)?l:"、"+l;
				}
				else{
					kxstr+="".equals(kxstr)?l+"("+l1+" "+f_name+")":"、"+l+"("+l1+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,n);
				f_name=obj.getString("five");
				n1=obj.getInt("kxnum");
				if (n1==0){
					str+="".equals(str)?n:"、"+n;
				}
				else{
					kxstr+="".equals(kxstr)?n+"("+n1+" "+f_name+")":"、"+n+"("+n1+" "+f_name+")";
				}
			}
			//单姓复名
			if (l.length()==1 && n.length()==2){
				type=1;
				
				obj=DataArray.GetWordJson(nametofortune.this,l);
				f_name=obj.getString("five");
				l1=obj.getInt("kxnum");
				if (l1==0){
					str+="".equals(str)?l:"、"+l;
				}
				else{
					kxstr+="".equals(kxstr)?l+"("+l1+" "+f_name+")":"、"+l+"("+l1+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,n.substring(0,1));
				f_name=obj.getString("five");
				n1=obj.getInt("kxnum");
				if (n1==0){
					str+="".equals(str)?n.substring(0,1):"、"+n.substring(0,1);
				}
				else{
					kxstr+="".equals(kxstr)?n.substring(0,1)+"("+n1+" "+f_name+")":"、"+n.substring(0,1)+"("+n1+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,n.substring(1,2));
				f_name=obj.getString("five");
				n2=obj.getInt("kxnum");
				if (n2==0){
					str+="".equals(str)?n.substring(1,2):"、"+n.substring(1,2);
				}
				else{
					kxstr+="".equals(kxstr)?n.substring(1,2)+"("+n2+" "+f_name+")":"、"+n.substring(1,2)+"("+n2+" "+f_name+")";
				}
			}
			//复姓单名
			if (l.length()==2 && n.length()==1){
				type=2;
				
				obj=DataArray.GetWordJson(nametofortune.this,l.substring(0,1));
				f_name=obj.getString("five");
				l1=obj.getInt("kxnum");
				if (l1==0){
					str+="".equals(str)?l.substring(0,1):"、"+l.substring(0,1);
				}
				else{
					kxstr+="".equals(kxstr)?l.substring(0,1)+"("+l1+" "+f_name+")":"、"+l.substring(0,1)+"("+l1+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,l.substring(1,2));
				f_name=obj.getString("five");
				l2=obj.getInt("kxnum");
				if (l2==0){
					str+="".equals(str)?l.substring(1,2):"、"+l.substring(1,2);
				}
				else{
					kxstr+="".equals(kxstr)?l.substring(1,2)+"("+l2+" "+f_name+")":"、"+l.substring(1,2)+"("+l2+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,n);
				f_name=obj.getString("five");
				n1=obj.getInt("kxnum");
				if (n1==0){
					str+="".equals(str)?n:"、"+n;
				}
				else{
					kxstr+="".equals(kxstr)?n+"("+n1+" "+f_name+")":"、"+n+"("+n1+" "+f_name+")";
				}
			}
			//复姓复名
			if (l.length()==2 && n.length()==2){
				type=3;
				
				obj=DataArray.GetWordJson(nametofortune.this,l.substring(0,1));
				f_name=obj.getString("five");
				l1=obj.getInt("kxnum");
				if (l1==0){
					str+="".equals(str)?l.substring(0,1):"、"+l.substring(0,1);
				}
				else{
					kxstr+="".equals(kxstr)?l.substring(0,1)+"("+l1+" "+f_name+")":"、"+l.substring(0,1)+"("+l1+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,l.substring(1,2));
				f_name=obj.getString("five");
				l2=obj.getInt("kxnum");
				if (l2==0){
					str+="".equals(str)?l.substring(1,2):"、"+l.substring(1,2);
				}
				else{
					kxstr+="".equals(kxstr)?l.substring(1,2)+"("+l2+" "+f_name+")":"、"+l.substring(1,2)+"("+l2+" "+f_name+")";
				}

				obj=DataArray.GetWordJson(nametofortune.this,n.substring(0,1));
				f_name=obj.getString("five");
				n1=obj.getInt("kxnum");
				if (n1==0){
					str+="".equals(str)?n.substring(0,1):"、"+n.substring(0,1);
				}
				else{
					kxstr+="".equals(kxstr)?n.substring(0,1)+"("+n1+" "+f_name+")":"、"+n.substring(0,1)+"("+n1+" "+f_name+")";
				}
				
				obj=DataArray.GetWordJson(nametofortune.this,n.substring(1,2));
				f_name=obj.getString("five");
				n2=obj.getInt("kxnum");
				if (n2==0){
					str+="".equals(str)?n.substring(1,2):"、"+n.substring(1,2);
				}
				else{
					kxstr+="".equals(kxstr)?n.substring(1,2)+"("+n2+" "+f_name+")":"、"+n.substring(1,2)+"("+n2+" "+f_name+")";
				}
			}
			Log.d(tag, "type:"+type+",l1:"+l1+",l2:"+l2+",n1:"+n1+",n2:"+n2);
			if (!"".equals(str)){
				str+="，无法查询笔画";
				Toast.makeText(nametofortune.this, str, Toast.LENGTH_LONG).show();
				return;
			}
			if (!"".equals(kxstr)){
				text.setText(kxstr);
			}
			namefortune(type);
		} catch (Exception e) {
			Log.e(tag, e.toString()+"");
			Toast.makeText(nametofortune.this, "姓名测算失败", Toast.LENGTH_LONG).show();
		}
	}
	
	private void namefortune(int type) throws Exception{
		switch (type) {
		case 0:
			c1=l1+1;
			c2=l1+n1;
			c3=n1+1;
			c4=2;
			c5=l1+n1;
			break;
		case 1:
			c1=l1+1;
			c2=l1+n1;
			c3=n1+n2;
			c4=n1+1;
			c5=l1+n1+n2;
			break;
		case 2:
			c1=l1+l2;
			c2=l2+n1;
			c3=n1+1;
			c4=l1+1;
			c5=l1+l2+n1;
			break;
		case 3:
			c1=l1+l2;
			c2=l2+n1;
			c3=n1+n2;
			c4=l1+n2;
			c5=l1+l2+n1+n2;
			break;
		}
		HashMap map=null;
		list=new ArrayList();
		String c6="";
		String five="";
		String text="";
		
		c6+=(c1+"").substring((c1+"").length()-1, (c1+"").length());
		c6+=(c2+"").substring((c2+"").length()-1, (c2+"").length());
		c6+=(c3+"").substring((c3+"").length()-1, (c3+"").length());
		
		five=DataArray.GetYinYang(c1)+DataArray.GetFive(c1);
		text=DataArray.GetNumber1(c1)+"\n\n"+DataArray.GetNumber2(c1);
		map=new HashMap ();
		map.put("title", "天格");
		map.put("c",c1);
		map.put("five", five);
		map.put("text", text);
		list.add(map);

		five=DataArray.GetYinYang(c2)+DataArray.GetFive(c2);
		text=DataArray.GetNumber1(c2)+"\n\n"+DataArray.GetNumber2(c2);
		map=new HashMap ();
		map.put("title", "人格");
		map.put("c",c2);
		map.put("five", five);
		map.put("text", text);
		list.add(map);
		
		five=DataArray.GetYinYang(c3)+DataArray.GetFive(c3);
		text=DataArray.GetNumber1(c3)+"\n\n"+DataArray.GetNumber2(c3);
		map=new HashMap ();
		map.put("title", "地格");
		map.put("c",c3);
		map.put("five", five);
		map.put("text", text);
		list.add(map);

		five=DataArray.GetYinYang(c4)+DataArray.GetFive(c4);
		text=DataArray.GetNumber1(c4)+"\n\n"+DataArray.GetNumber2(c4);
		map=new HashMap ();
		map.put("title", "外格");
		map.put("c",c4);
		map.put("five", five);
		map.put("text", text);
		list.add(map);

		five=DataArray.GetYinYang(c5)+DataArray.GetFive(c5);
		text=DataArray.GetNumber1(c5)+"\n\n"+DataArray.GetNumber2(c5);
		map=new HashMap ();
		map.put("title", "总格");
		map.put("c",c5);
		map.put("five", five);
		map.put("text", text);
		list.add(map);

		five=DataArray.GetFive(c1)+DataArray.GetFive(c2)+DataArray.GetFive(c3);
		text=DataArray.GetSanCai(five);
		map=new HashMap ();
		map.put("title", "三才");
		map.put("c",c6);
		map.put("five", five);
		map.put("text", text);
		list.add(map);
		
		dataAdapter = new ViewAdapter(nametofortune.this, R.layout.list,list);
		listView.setAdapter(dataAdapter);
	}
	
	private class ViewAdapter extends BaseAdapter {
		private Context mContext;
		private List list;
		private int textViewResourceId;

		public ViewAdapter(Context c,int textViewResourceId, List list) {
			this.mContext = c;
			this.textViewResourceId = textViewResourceId;
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public HashMap getItem(int position) {
			return (HashMap)list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater vi = (LayoutInflater) nametofortune.this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(textViewResourceId, null);
			}
			HashMap map=getItem(position);
			
			title1=(TextView) convertView.findViewById(R.id.title1);
			title1.getPaint().setFakeBoldText(true);
			title2=(TextView) convertView.findViewById(R.id.title2);
			title2.getPaint().setFakeBoldText(true);
			title3=(TextView) convertView.findViewById(R.id.title3);
			title3.getPaint().setFakeBoldText(true);
			
			c=(TextView) convertView.findViewById(R.id.c);
			five=(TextView) convertView.findViewById(R.id.five);
			text1=(TextView) convertView.findViewById(R.id.text1);
			
			title1.setText(map.get("title")+"");
			c.setText(map.get("c")+"");
			five.setText(map.get("five")+"");
			int color=CommonUtil.GetFiveColor(map.get("five")+"");
			five.setTextColor(getResources().getColor(color));
			text1.setText(map.get("text")+"");
			return convertView;
		}
	}
}
